/*
* $Id: TreeSettings.cpp 143 2010-09-09 19:55:18Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "../UserMessage.h"
#include "Settings/InterfaceStn.h"
#include "TreeSettings.h"
#include "SettingsWin.h"

CTreeSettings::CTreeSettings(CSettingsWin* pParent){
	m_pParent = pParent;
	m_Bitmap.LoadBitmap(IDB_BEE);
	m_pFontHeader = CCommonFunction::GetFont(_T("Microsoft Sans Serif"), 16, TRUE, FALSE);
	m_pFontElement = CCommonFunction::GetFont(_T("Microsoft Sans Serif"), 14, FALSE, FALSE);
	std::vector<ISetting*> aSettings = ISettingsManager::GetSettingsManager()->GetSettings();
	for(std::vector<ISetting*>::iterator it = aSettings.begin(); it != aSettings.end(); it++)
			m_aItems.push_back(Item((*it)->GetNameSetting(), (*it)->IsHeader()));
}
CTreeSettings::~CTreeSettings(){
	m_Bitmap.DeleteObject();
	m_pFontElement->DeleteObject();
	delete m_pFontElement;
	m_pFontHeader->DeleteObject();
	delete m_pFontHeader;
}

void CTreeSettings::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct){
	LPCTSTR lpszText = (LPCTSTR) lpMeasureItemStruct->itemData;
	BITMAP bmp;
	m_Bitmap.GetBitmap(&bmp);
	lpMeasureItemStruct->itemHeight = bmp.bmHeight + 4;
}
void CTreeSettings::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct){
	int nCount = GetCount();
	if(nCount)
	{
		LPCTSTR lpszText = ((Item*)lpDrawItemStruct->itemData)->m_strValueItem.c_str();
		bool bHeader = ((Item*)lpDrawItemStruct->itemData)->m_bHeader;
		CDC dc;
		dc.Attach(lpDrawItemStruct->hDC);
		CBrush brush;
		brush.CreateSolidBrush(RGB(255, 255, 255));
		dc.FillRect(&lpDrawItemStruct->rcItem, &brush);
		brush.DeleteObject();
		if ((lpDrawItemStruct->itemState & ODS_SELECTED)){
			COLORREF color = CCommonFunction::GetBeeColor();
			CRect rect(lpDrawItemStruct->rcItem);
			rect.DeflateRect(1, 1, 1, 1);
			CPen pen;
			pen.CreatePen(PS_DOT, 1, color);
			HGDIOBJ penObj = dc.SelectObject(pen);
			dc.Rectangle(rect);
			dc.SelectObject(penObj);

			dc.SetTextColor(color);
			brush.CreateSolidBrush(color);
		}else{
			COLORREF color = RGB(0, 0, 0);
			dc.SetTextColor(color);
			brush.CreateSolidBrush(color);
		}
		BITMAP bmp;
		m_Bitmap.GetBitmap(&bmp);
		CRect rect(lpDrawItemStruct->rcItem);
		dc.SetBkMode(TRANSPARENT);
		if(bHeader){
			dc.DrawState(CPoint(lpDrawItemStruct->rcItem.left + 2, lpDrawItemStruct->rcItem.top + 2),
				CSize(bmp.bmWidth, bmp.bmHeight), &m_Bitmap, DST_BITMAP | DSS_NORMAL, NULL);
			dc.SelectObject(m_pFontHeader);
			rect.DeflateRect(bmp.bmWidth, 0, 0, 0);
		}else{
			CRect rectTemp(30, rect.top + bmp.bmHeight/2, 35, rect.bottom - bmp.bmHeight/2);
			CPen pen;
			LOGBRUSH lb;
			brush.GetLogBrush(&lb);
			pen.CreatePen(PS_SOLID, 1, lb.lbColor);
			HGDIOBJ brushObj = dc.SelectObject(brush);
			HGDIOBJ penObj = dc.SelectObject(pen);
			dc.Rectangle(rectTemp);
			dc.SelectObject(penObj);
			dc.SelectObject(brushObj);
			dc.SelectObject(m_pFontElement);
			rect.DeflateRect(40, 0, 0, 0);
		}
		dc.DrawText(lpszText, (int)_tcslen(lpszText), rect, DT_LEFT|DT_SINGLELINE|DT_VCENTER);
		dc.Detach();
	}
}

BEGIN_MESSAGE_MAP(CTreeSettings, CListBox)
	ON_WM_CREATE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_RBUTTONUP()
	ON_WM_RBUTTONDOWN()
	ON_WM_SETFOCUS()
	ON_WM_KEYDOWN()
END_MESSAGE_MAP()

int CTreeSettings::OnCreate(LPCREATESTRUCT lpCreateStruct){
	int nRes = __super::OnCreate(lpCreateStruct);
	for(std::vector<Item>::iterator it = m_aItems.begin(); it != m_aItems.end(); it++)
		SetItemData(AddString(_T("")), (DWORD_PTR)&*it);
	return nRes;
}
void CTreeSettings::OnLButtonDblClk(UINT nFlags, CPoint point){
	BOOL bOutside;
	int nIndex = ItemFromPoint(point, bOutside);
	if(!bOutside){
		Item* item = (Item*)GetItemData(nIndex);
		nIndex++;
		int Count = GetCount();
		if(item->m_bHeader){
			bool bAdd = false;
			Item* itemNext = (Item*)GetItemData(nIndex);
			if(Count <= nIndex || itemNext->m_bHeader)
				bAdd = true;
			std::vector<Item>::iterator it = std::find(m_aItems.begin(), m_aItems.end(), item->m_strValueItem);
			while(true){
				if(!bAdd){
					DeleteString(nIndex);
					itemNext = (Item*)GetItemData(nIndex);
					Count = GetCount();
					if(Count <= nIndex || itemNext->m_bHeader)
						break;
				}else{
					it++;
					if(it == m_aItems.end() || it->m_bHeader)
						break;
					InsertString(nIndex, _T(""));
					SetItemData(nIndex, (DWORD_PTR)&*it);
					nIndex++;
				}
			}
		}
	}
}
void CTreeSettings::OnRButtonDown(UINT nFlags, CPoint point){
	SendMessage(WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(point.x, point.y));
}
void CTreeSettings::OnRButtonUp(UINT nFlags, CPoint point){
	SendMessage(WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(point.x, point.y));
}
void CTreeSettings::OnLButtonDown(UINT nFlags, CPoint point){
	BOOL bOutside;
	int nIndex = ItemFromPoint(point, bOutside);
	if(!bOutside && m_pParent->IsValidate()){
		SetCurSel(nIndex);
		__super::OnLButtonDown(nFlags, nIndex);
	}
}
void CTreeSettings::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags){
	if(nChar != VK_UP && nChar != VK_DOWN && nChar != VK_RIGHT && nChar != VK_LEFT || m_pParent->IsValidate())
		__super::OnKeyDown(nChar, nRepCnt, nFlags);
	if(nChar == VK_TAB)
		GetParent()->SendMessage(UM_TABSTOP, 0, (LPARAM)this);
}
void CTreeSettings::OnSetFocus(CWnd* pOldWnd){
	if(GetCurSel() == -1){
		SetCurSel(0);
		GetParent()->SendMessage(WM_COMMAND, MAKEWPARAM(TREE_SETTINGS_ID, LBN_SELCHANGE), (LPARAM)m_hWnd);
	}
	__super::OnSetFocus(pOldWnd);
}